package com.lmxdawn.api.dnf.listener;

import com.alibaba.fastjson.JSON;
import com.lmxdawn.api.dnf.config.MQConfig;
import com.lmxdawn.api.dnf.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class OrderPayListener {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = MQConfig.ORDER_PAY)
    public void receivePayMessage(String message){
        log.info("received order pay msg:{}", message);
        Map map = JSON.parseObject(message, Map.class);
        //调用业务层,完成订单数据库的修改
        orderService.updatePayStatus((String)map.get("orderId"),(String)map.get("transactionId"));
    }

    @RabbitListener(queues = MQConfig.ORDER_TIMEOUT_QUEUE)
    public void receiveOrderTimeoutMessage(String orderId){
        log.info("received order timeout msg:{}", orderId);
        //删除订单,库存回调
        orderService.closeOrder(orderId);
   }
}
